/**
 * 处理错误信息，对错误信息进行国际化处理
 */

import codeMessage from "./codeMessage";
import { RequestResultType } from "./resultFormat";

const errorHandler = (response: Response | undefined): RequestResultType<string> => {
	let msg = "";
	let code = "9999";
	let content = "";
	if (response && response.status) {
		const errorText = codeMessage[response.status] || response.statusText;
		const { status, url } = response;
		msg = `Request error ${status}: ${url}`;
		content = errorText;
		code = status.toString();
	} else if (!response) {
		msg = "Network anomaly";
		content = "Your network is abnormal and cannot connect to the server";
	}
	return {
		isSuccess: false,
		code,
		msg,
		content,
	};
};

export default errorHandler;
